class BarGuest < ActiveRecord::Base
  belongs_to :bar
  belongs_to :user
  
  validates_presence_of :user_id
  validates_presence_of :bar_id 
  
  def self.add_guest(bar_id, user_id)
    # first check for duplictate
    duplicate = find(:all, :conditions => ["bar_id = ? and user_id = ?", 
                                           bar_id, user_id])
    if duplicate.blank?                                       
      guest         = BarGuest.new
      guest.bar_id  = bar_id
      guest.user_id = user_id
      guest.save! 
      
      # Update user's bars count
      user = User.find(user_id)
      user.user_bars_count = user.user_bars_count.to_i + 1
      user.save!
      
      return true
    else
      return false
    end
  end
end
